(function(win, doc) {
  function Form(options) {
    var targerClass = '.simulation_checkbox',
      disabledClass = '.simulation_checkbox_disabled',
      activeClass = '.simulation_checkbox_active';

    function normalizeEventName(eventName) {
      if (eventName.indexOf('on') !== 0) {
        if (eventName[0] !== eventName[0].toUpperCase()) {
          eventName = 'on' + eventName[0].toUpperCase() + eventName.substring(1);
        } else {
          eventName = 'on' + eventName;
        }
      }
      return eventName;
    }
    return {
      init: function() {
        this.emitterEventListeners = {};
        this.emit = function(eventName) {
          // Trigger callbacks
          if (this.params[eventName]) {
            this.params[eventName](arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
          }
          var i;
          // Trigger events
          if (this.emitterEventListeners[eventName]) {
            for (i = 0; i < this.emitterEventListeners[eventName].length; i++) {
              this.emitterEventListeners[eventName][i](arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
            }
          }
        };
        this.on = function(eventName, handler) {
          eventName = normalizeEventName(eventName);
          if (!this.emitterEventListeners[eventName]) this.emitterEventListeners[eventName] = [];
          this.emittserEventListeners[eventName].push(handler);
          return this;
        };
        return this;
      },
      Radio: function(container, options) {
        this.ele = typeof container === 'object' ? container : document.querySelectorAll(container);
        this.params = options || {};
        var _this = this;
        [].forEach.call(this.ele, function(ele, index, arr) {
          ele.addEventListener('click', function() {
              var is = ele.querySelector(targerClass).classList.contains(activeClass.slice(1));
              if (is) {
                return;
              }
              _this.emit('onchange', _this, this);
              [].forEach.call(_this.ele, function(ele, index, arr) {
                ele.querySelector(targerClass).classList.remove(activeClass.slice(1));
              })
              this.querySelector(targerClass).classList.add(activeClass.slice(1));

            },
            false);
        });
        return this;
      },
      Checkbox: function(container, options) {
        this.ele = typeof container === 'object' ? container : document.querySelectorAll(container);
        this.params = options || {};
        this.iSelect = this.params.iSelect;
        var _this = this;
        [].forEach.call(this.ele, function(ele, index, arr) {
          ele.addEventListener('click', function() {
              var is = ele.querySelector(targerClass).classList.contains(activeClass.slice(1)),
                disabled = ele.querySelector(targerClass).classList.contains(disabledClass.slice(1));
              if (!disabled) {
                if (is) {
                  this.querySelector(targerClass).classList.remove(activeClass.slice(1));
                  _this.iSelect = !is;
                } else {
                  this.querySelector(targerClass).classList.add(activeClass.slice(1));
                  _this.iSelect = !is;
                }
                _this.emit('onchange', _this, this);
              }

            },
            false);
        });
        return this;
      }
    }
  };
  window.form = Form().init();
}(window, document));
